<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>行业动态列表 - 测盟汇系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        :root {
            --primary-color: #2c5545;
            --primary-light: #3a7d62;
            --primary-hover: #1e4636;
            --accent-color: #8ba888;
            --error-color: #d64541;
            --success-color: #3a7d62;
            --text-color: #333;
            --text-light: #555;
            --border-color: #d1d9d6;
            --bg-light: #f5f7f6;
            --bg-page: #e8ecea;
            --white: #fff;
            --shadow: 0 4px 24px rgba(44, 85, 69, 0.1);
            --radius: 12px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            line-height: 1.6;
            background-color: var(--bg-page);
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 250px;
            background: var(--primary-color);
            color: var(--white);
            padding: 1.5rem 0;
            height: 100vh;
            position: fixed;
            transition: var(--transition);
            z-index: 100;
        }

        .sidebar-header {
            padding: 0 1.5rem 1.5rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .sidebar-header h3 {
            display: flex;
            align-items: center;
            font-weight: 600;
        }

        .sidebar-header h3 i {
            margin-right: 0.5rem;
            font-size: 1.2rem;
        }

        .sidebar-menu {
            padding: 1rem 0;
        }

        .menu-title {
            padding: 0.5rem 1.5rem;
            font-size: 0.8rem;
            text-transform: uppercase;
            color: rgba(255, 255, 255, 0.5);
            letter-spacing: 1px;
        }

        .menu-item {
            padding: 0.75rem 1.5rem;
            display: flex;
            align-items: center;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: var(--transition);
        }

        .menu-item i {
            margin-right: 0.8rem;
            width: 20px;
            text-align: center;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
            color: var(--white);
        }

        .menu-item.active {
            background: var(--primary-hover);
            color: var(--white);
            border-left: 3px solid var(--accent-color);
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 2rem;
            transition: var(--transition);
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--border-color);
        }

        .header h1 {
            color: var(--primary-color);
            font-size: 1.8rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .back-btn {
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 50%;
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: var(--transition);
        }

        .back-btn:hover {
            background: var(--primary-hover);
            transform: translateX(-2px);
        }

        .user-profile {
            display: flex;
            align-items: center;
        }

        .user-profile img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 0.8rem;
            object-fit: cover;
        }

        .user-info {
            display: flex;
            flex-direction: column;
        }

        .user-name {
            font-weight: 500;
        }

        .user-role {
            font-size: 0.8rem;
            color: var(--text-light);
        }

        /* 列表内容样式 */
        .content-container {
            background: var(--white);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 2rem;
        }

        .search-container {
            display: flex;
            gap: 12px;
            align-items: center;
            margin-bottom: 25px;
            flex-wrap: wrap;
            background-color: var(--bg-light);
            padding: 15px;
            border-radius: var(--radius);
        }

        .table {
            margin-bottom: 25px;
            border-collapse: separate;
            border-spacing: 0;
            width: 100%;
        }

        .table thead th {
            background-color: var(--primary-color);
            color: white;
            border: none;
            font-weight: 500;
            padding: 12px 15px;
        }

        .table tbody tr {
            transition: all 0.2s ease;
        }

        .table tbody tr:hover {
            background-color: rgba(44, 85, 69, 0.05);
            transform: translateY(-1px);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
        }

        .table td, .table th {
            vertical-align: middle;
            padding: 12px 15px;
            border-bottom: 1px solid var(--border-color);
        }

        .table td a {
            color: var(--primary-light);
            text-decoration: none;
            font-weight: 500;
            transition: color 0.2s;
        }

        .table td a:hover {
            color: var(--primary-color);
            text-decoration: underline;
        }

        .cover-image {
            width: 100px;
            height: 70px;
            object-fit: cover;
            border-radius: 6px;
            transition: transform 0.3s ease;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .cover-image:hover {
            transform: scale(1.05);
        }

        .btn {
            border-radius: 6px;
            font-weight: 500;
            padding: 8px 15px;
            transition: all 0.2s ease;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            border: none;
        }

        .btn i {
            font-size: 0.9em;
        }

        .btn-primary {
            background-color: var(--primary-color);
        }

        .btn-primary:hover {
            background-color: var(--primary-hover);
            transform: translateY(-1px);
        }

        .btn-success {
            background-color: var(--success-color);
        }

        .btn-success:hover {
            background-color: #2d6b4d;
            transform: translateY(-1px);
        }

        .btn-warning {
            background-color: #90ddc0;
        }

        .btn-warning:hover {
            background-color: #d1a871;
            transform: translateY(-1px);
        }

        .btn-danger {
            background-color: var(--error-color);
        }

        .btn-danger:hover {
            background-color: #c53d39;
            transform: translateY(-1px);
        }

        .btn-secondary {
            background-color: #6c757d;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-1px);
        }

        .pagination-container {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }

        .form-control {
            border-radius: 6px;
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.25rem rgba(44, 85, 69, 0.25);
        }

        .no-results {
            text-align: center;
            padding: 30px;
            color: var(--text-light);
            font-size: 1.1em;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                overflow: hidden;
            }

            .sidebar-header h3 span,
            .menu-title,
            .menu-item span {
                display: none;
            }

            .menu-item {
                justify-content: center;
                padding: 0.75rem 0;
            }

            .menu-item i {
                margin-right: 0;
                font-size: 1.2rem;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .main-content {
                padding: 1.5rem;
            }

            .search-container {
                flex-direction: column;
                align-items: stretch;
            }

            .search-container input, .search-container button {
                width: 100% !important;
            }

            .content-container {
                padding: 1rem;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航栏 -->
<div class="sidebar">
    <div class="sidebar-header">
        <h3><i class="fas fa-store"></i> <span>测盟汇系统</span></h3>
    </div>

    <div class="sidebar-menu">
        <div class="menu-title">核心功能</div>
        <a href="/index" class="menu-item">
            <i class="fas fa-tachometer-alt"></i>
            <span>控制面板</span>
        </a>

        <div class="menu-title">功能模块</div>
        <a href="#" class="menu-item">
            <i class="fas fa-book-open"></i>
            <span>课程</span>
        </a>
        <a href="/dynamic_list" class="menu-item active">
            <i class="fas fa-bullhorn"></i>
            <span>动态</span>
        </a>
        <a href="#" class="menu-item">
            <i class="fas fa-video"></i>
            <span>会议</span>
        </a>

        <div class="menu-title">系统设置</div>
        <a href="/user/profile" class="menu-item">
            <i class="fas fa-user-cog"></i>
            <span>个人资料</span>
        </a>
        <a href="/enterprise/profile" class="menu-item">
            <i class="fas fa-building"></i>
            <span>企业资料修改</span>
        </a>
    </div>
</div>

<!-- 主内容区域 -->
<div class="main-content">
    <div class="header">
        <h1>
            <button class="back-btn" onclick="window.history.back()">
                <i class="fas fa-arrow-left"></i>
            </button>
            <i class="fas fa-bullhorn"></i> 行业动态列表
        </h1>
        <div class="user-profile">
            <img th:src="@{/images/default-avatar.jpg}" alt="用户头像">
            <div class="user-info">
                <span class="user-name" th:text="${session.user.nickname ?: session.user.username}">用户名</span>
                <span class="user-role" th:text="${session.user.isSuperAdmin ? '超级管理员' : '普通用户'}">用户角色</span>
            </div>
        </div>
    </div>

    <div class="content-container">
        <div class="search-container">
            <input type="text" id="keyword" class="form-control" placeholder="搜索关键字" style="width: 220px;">
            <input type="text" id="author" class="form-control" placeholder="作者" style="width: 180px;">
            <button class="btn btn-primary" onclick="searchDynamics()">
                <i class="fas fa-search"></i> 搜索
            </button>
            <button class="btn btn-secondary" onclick="resetSearch()">
                <i class="fas fa-sync-alt"></i> 重置
            </button>
            <button class="btn btn-info" onclick="showDrafts()">
                <i class="fas fa-file-alt"></i> 草稿箱
            </button>
            <a href="/add" class="btn btn-success">
                <i class="fas fa-plus-circle"></i> 发布动态
            </a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>封面</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>发布时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="dynamicList">
                </tbody>
            </table>
        </div>
        <div class="pagination-container">
            <button class="btn btn-primary" onclick="prevPage()">
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            <button class="btn btn-primary" onclick="nextPage()">
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
        </div>
    </div>
</div>

<script>
    let currentPage = 1;
    const pageSize = 3;
    let currentStatus = 1; // 1表示已发布，0表示草稿

    // 加载动态列表
    function loadDynamics(keyword = '', author = '', status = 1, page = 1) {
        currentPage = page;
        $.ajax({
            url: '/dynamic/list',
            method: 'GET',
            data: {
                page: currentPage,
                pageSize: pageSize,
                keyword: keyword,
                author: author,
                status: status
            },
            success: function(response) {
                if (response.code === 200) {
                    const dynamics = response.data;
                    let html = '';

                    // 处理空列表情况
                    if (dynamics.length === 0) {
                        html = `
              <tr>
                <td colspan="6" class="no-results">
                  <i class="fas fa-exclamation-circle" style="font-size: 1.5em;"></i><br>
                  暂无符合条件的动态
                </td>
              </tr>
            `;
                    } else {
                        dynamics.forEach(dynamic => {
                            const coverUrl = dynamic.coverUrl ? dynamic.coverUrl : 'https://via.placeholder.com/100x70?text=No+Image';
                            const publishTime = formatDate(dynamic.publishTime);

                            html += `
                <tr>
                  <td>${dynamic.id}</td>
                  <td>
                    <img src="${coverUrl}" class="cover-image" alt="封面图">
                  </td>
                  <td><a href="/detail?id=${dynamic.id}">${dynamic.title}</a></td>
                  <td>${dynamic.author || '未知作者'}</td>
                  <td>${publishTime}</td>
                  <td>
                    <a href="/edit?id=${dynamic.id}" class="btn btn-warning btn-sm">
                      <i class="fas fa-edit"></i> 编辑
                    </a>
                    <button class="btn btn-danger btn-sm" onclick="deleteDynamic(${dynamic.id})">
                      <i class="fas fa-trash-alt"></i> 删除
                    </button>
                  </td>
                </tr>
              `;
                        });
                    }

                    $('#dynamicList').html(html);
                } else {
                    alert(response.message);
                }
            },
            error: function() {
                alert('查询失败，请刷新页面重试');
            }
        });
    }

    // 搜索功能
    function searchDynamics() {
        const keyword = $('#keyword').val().trim();
        const author = $('#author').val().trim();
        currentPage = 1;
        loadDynamics(keyword, author);
    }

    // 重置搜索
    function resetSearch() {
        $('#keyword').val('');
        $('#author').val('');
        currentPage = 1;
        loadDynamics();
    }

    // 删除动态
    function deleteDynamic(id) {
        if (confirm('确定要删除该动态吗？删除后不可恢复！')) {
            $.ajax({
                url: '/dynamic/delete',
                method: 'POST',
                data: { dynamicId: id },
                success: function(response) {
                    if (response.code === 200) {
                        alert('删除成功');
                        loadDynamics($('#keyword').val().trim(), $('#author').val().trim());
                    } else {
                        alert('删除失败：' + response.message);
                    }
                },
                error: function() {
                    alert('删除请求失败');
                }
            });
        }
    }

    // 辅助函数：格式化日期显示
    function formatDate(dateStr) {
        if (!dateStr) return '未知时间';
        const date = new Date(dateStr);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    }

    // 显示草稿箱动态
    function showDrafts() {
        const keyword = $('#keyword').val().trim();
        const author = $('#author').val().trim();
        currentPage = 1;
        currentStatus = 0; // 切换到草稿状态
        loadDynamics(keyword, author, currentStatus, currentPage);
    }

    // 上一页
    function prevPage() {
        if (currentPage > 1) {
            currentPage--;
            const keyword = $('#keyword').val().trim();
            const author = $('#author').val().trim();
            // 使用当前状态（草稿/已发布）
            loadDynamics(keyword, author, currentStatus, currentPage);
        }
    }

    // 下一页
    function nextPage() {
        currentPage++;
        const keyword = $('#keyword').val().trim();
        const author = $('#author').val().trim();
        // 使用当前状态（草稿/已发布）
        loadDynamics(keyword, author, currentStatus, currentPage);
    }

    // 页面加载时初始化列表
    $(document).ready(function() {
        currentStatus = 1; // 默认显示已发布
        loadDynamics();
    });

</script>
</body>
</html>